﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using UnityEngine;

/// <summary>
/// 结构解析
/// </summary>
public class InitXML
{
    private static InitXML self;

    //private List<string> InitCompleteXml = new List<string>();

    public InitXML() { }

    public static InitXML getInstance()
    {
        if (self == null)
        {
            self = new InitXML();
        }
        return self;
    }
    /// <summary>
    /// 本地初始化对象
    /// </summary>
    /// <param name="xmlName">结构表</param>
    /// <param name="dbTableName">数据库配置表名</param>
    /// <param name="configKey">数据库配置文件的查找key值</param>
    /// <param name="sceneObj">数据对象</param>
    public void AnalyzeXml(string xmlName, ObjectData sceneObj)
    {
        AnalyzeXml(xmlName, "", "", "", sceneObj);
    }
    public void AnalyzeXml(string xmlName, string dbTableName, string checkKey, string configKey, ObjectData sceneObj)
    {
        String[] xmlNameArr = xmlName.Split(new string[] { "\\", "." }, StringSplitOptions.RemoveEmptyEntries);
        xmlName = xmlNameArr[xmlNameArr.Length - 2];
        string path = Application.dataPath + "/res/" + xmlName + ".xml";

        //if (InitCompleteXml.IndexOf(path) == -1)
        //{q
        //    InitCompleteXml.Add(path);

        XmlReader reader = null;
        try
        {
            // 创建XmlReaderSettings对象
            XmlReaderSettings settings = new XmlReaderSettings();
            // 设置XmlReaderSettings对象的属性
            settings.ProhibitDtd = false;
            // 使用XmlReaderSettings对象来创建XmlReader对象
            reader = XmlReader.Create(path, settings);

            if (dbTableName != "")
            {
                String[] dbTableNameArr = dbTableName.Split(new string[] { "\\", "." }, StringSplitOptions.RemoveEmptyEntries);
                dbTableName = dbTableNameArr[dbTableNameArr.Length - 2];
            }


            ReadXml(reader, dbTableName, checkKey, configKey, sceneObj);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
        //}
    }

    private void ReadXml(XmlReader reader, string dbTableName, string checkKey, string configKey, ObjectData sceneObj)
    {
        try
        {
            int propertyIndex = 0;
            string recordName = "";
            Record record = new Record("0");
            List<int> arrType = new List<int>();
            // 解析文件，并显示每一个节点
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (reader.Name == "field")
                        {
                            if (reader.HasAttributes)   // 属性
                            {
                                string key = "";
                                string keyValue = "";
                                int type = 1;
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "name")//唯一属性
                                    {
                                        key = reader.Value;
                                    }
                                    else if (reader.Name == "value")//属性值
                                    {
                                        keyValue = reader.Value;
                                    }
                                    else if (reader.Name == "type")//属性类型
                                    {
                                        switch (reader.Value)
                                        {
                                            case "int32":
                                                type = (int)DataType.Value_Type.VARTYPE_INT32;
                                                break;
                                            case "string":
                                                type = (int)DataType.Value_Type.VARTYPE_STRING;
                                                break;
                                            case "widestr":
                                                type = (int)DataType.Value_Type.VARTYPE_WIDESTR;
                                                break;
                                            case "object":
                                                type = (int)DataType.Value_Type.VARTYPE_OBJECT;
                                                break;
                                        }
                                    }
                                }
                                if (configKey == "" || dbTableName == "")
                                {
                                    sceneObj.HandlePropertiesEx(key, keyValue);
                                }
                                else
                                {
                                    sceneObj.HandlePropertiesEx(key, SqliteReader.getInstance().getIniVal(dbTableName, checkKey, configKey, new string[] { key })[0]);//需从配置文件读取(配置文件从sceneObj相关信息中取)
									Debug.Log(key);
                                }
                                sceneObj.ProertyTypes.HandleProperties(key, type);
                                propertyIndex++;
                            }
                        }
                        else if (reader.Name == "record")
                        {
                            int type = 13;//定义表格类型//
                            if (reader.HasAttributes)   // 属性
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "name")
                                    {
                                        recordName = reader.Value;
                                    }
                                    else if (reader.Name == "type")//属性类型
                                    {
                                        type = 13;
                                    }
                                }
                            }

                            if (arrType.Count != 0)
                            {
                                List<int> typeArr = new List<int>(arrType);
                                record.SetColType(typeArr);
                               	record.initRecordSelf(recordName);//读取表 初始化 需要知道关联的表名
                                sceneObj.ProertyTypes.HandleProperties(recordName, type);
                                sceneObj.HandleProperties(recordName, record);
                                arrType.Clear();
                            }
                            record = new Record(recordName);
                            break;
                        }
                        else if (reader.Name == "column")
                        {
                            if (reader.HasAttributes)   // 属性
                            {
                                int type = 1;
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "type")
                                    {
                                        switch (reader.Value)
                                        {
                                            case "int32":
                                                type = (int)DataType.Value_Type.VARTYPE_INT32;
                                                break;
                                            case "string":
                                                type = (int)DataType.Value_Type.VARTYPE_STRING;
                                                break;
                                            case "widestr":
                                                type = (int)DataType.Value_Type.VARTYPE_WIDESTR;
                                                break;
                                            case "object":
                                                type = (int)DataType.Value_Type.VARTYPE_OBJECT;
                                                break;
                                        }
                                        arrType.Add(type);
                                    }
                                }
                            }
                            break;
                        }




                        break;
                }
            }
        }
        catch (XmlException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }



}